#pragma once
/*
@description: Singleton to get device
@author: TriLVH
*/
#include <d3dx9.h>
#include "Viewport.h"
#include "GameLogic.h"
#include "GameBullet.h"

class GameDeviceSingleton {
private:
	LPDIRECT3DDEVICE9 *_d3ddv;
	LPD3DXSPRITE *_spriteHandler;
	LPDIRECT3DSURFACE9 *_background;
	D3DXMATRIX* _pMatrix;
	GameLogic* _logic;
	CViewPort* _viewport;
	double _mapHeight;
	double _mapWidth;
	int _currentEntityId;
	int _currentMapLevel;
	Rect* playerRect;
	GameBullet* gameBullets;

	static GameDeviceSingleton* _singleton;
	GameDeviceSingleton() {
		_currentEntityId = 1;
	}
public:
	static GameDeviceSingleton*  getInstance() {
		return _singleton;
	}

	GameLogic* getLogic() {
		return _logic;
	}

	static void initDevice(LPDIRECT3DDEVICE9 *_d3ddv, LPD3DXSPRITE *_spriteHandler, LPDIRECT3DSURFACE9 *_background) {
		_singleton->_d3ddv = _d3ddv;
		_singleton->_spriteHandler = _spriteHandler;
		_singleton->_background = _background;
		_singleton->_logic = new GameLogic(*(_spriteHandler));
		_singleton->gameBullets = new GameBullet();
		_singleton->_currentMapLevel = _singleton->_preMapLevel = 1;
	}

	void setViewPort(CViewPort* viewport) {
		_singleton->_viewport = viewport;
		_logic->setViewportRect(viewport->getCurView());
	}

	void setMapWiHi(double width, double height) {
		_singleton->_mapHeight = height;
		_singleton->_mapWidth = width;
	}

	double getMapWidth() {
		return _mapWidth;
	}

	double getMapHeight() {
		return _mapHeight;
	}

	CViewPort* getViewPort() {
		return _singleton->_viewport;
	}

	LPDIRECT3DDEVICE9 * getD3DV() {
		if (_singleton == NULL) {
			return NULL;
		}
		return _singleton->_d3ddv;
	}

	LPD3DXSPRITE * getSpriteHandler() {
		if (_singleton == NULL) {
			return NULL;
		}
		return _singleton->_spriteHandler;
	}

	LPDIRECT3DSURFACE9 *getBackground() {
		if (_singleton == NULL) {
			return NULL;
		}
		return _singleton->_background;
	}

	D3DXMATRIX* getMatrix() {
		if (_singleton == NULL) {
			return NULL;
		}
		return _singleton->_pMatrix;
	}

	void setMatrix(D3DXMATRIX *pMatrix) {
		if (_singleton == NULL) {
			return;
		}
		else {
			_singleton->_pMatrix = pMatrix;
		}
	}

	Rect* getPlayerRect() {
		return playerRect;
	}

	void setPlayerRect(Rect* rect) {
		playerRect = rect;
	}

	int getCurrentEntityId() {
		_currentEntityId++;
		return _currentEntityId;
	}

	int getCurrenMapLevel() {
		_preMapLevel = _currentMapLevel;
		return _currentMapLevel;
	}

	bool checkOnChangeMap() {
		bool res =  _currentMapLevel != _preMapLevel;
		_preMapLevel = _currentMapLevel;
		return res;
	}

	int _preMapLevel;
	void setCurrentMapLevel(int level) {
		_preMapLevel = _currentMapLevel;
		_currentMapLevel = level;
	}

	void drawLogic() {
		_logic->draw();
	}

	GameBullet* getGameBullet() {
		return gameBullets;
	}
};